package com.xci.core.aspect;

import com.xci.core.annotation.CurrentUser;
import com.xci.core.internal.Sys;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 设置当前用户拦截器
 *
 * @author 吕艳阳
 */
@Aspect
@Slf4j
public class CurrentUserAspect {
    @Before("@annotation(com.xci.core.annotation.CurrentUser)")
    public void doProcess(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        if (args.length == 0) return;
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        CurrentUser cu = method.getAnnotation(CurrentUser.class);
        for (Object bean : args) {
            if (bean == null) continue;
            if (bean.getClass().isPrimitive()) continue;
            Sys.me().setUserInfo(bean, cu.created());
        }
    }
}